/*************************************************************************
 * File Name:    A.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: Sat 12 Oct 2013 09:01:54 AM CST
 * 
 * Description:  
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

#define MAX 100

int matrix[MAX][MAX];

bool
check_row(int row, int len)
{
    set<int> s;

    for (int i = 1; i <= len; ++i) {
        if (matrix[row][i] <= 0 || matrix[row][i] > len) return false;
        if (s.find(matrix[row][i]) != s.end()) return false;
        s.insert(matrix[row][i]);
    }
    return true;
}

bool
check_col(int col, int len)
{
    set<int> s;

    for (int i = 1; i <= len; ++i) {
        if (matrix[i][col] <= 0 || matrix[i][col] > len) return false;
        if (s.find(matrix[i][col]) != s.end()) return false;
        s.insert(matrix[i][col]);
    }
    return true;
}

bool
check_matrix(int id, int N)
{
    set<int> s;
    int row = id / N;
    int col = id % N;

    for (int i = row * N + 1; i <= (row + 1) * N; ++i) {
        for (int j = col * N + 1; j <= (col + 1) * N; ++j) {
            if (matrix[i][j] <= 0 || matrix[i][j] > N * N) return false;
            if (s.find(matrix[i][j]) != s.end()) return false;
            s.insert(matrix[i][j]);
        }
    }
    return true;
}

int
main(int argc, char *argv[])
{
    //std::ios_base::sync_with_stdio(false);

    int T, N;
    bool ans;

    cin >> T;
    for (int k = 1; k <= T; ++k) {
        cin >> N;
        for (int i = 1; i <= N * N; ++i) {
            for (int j = 1; j <= N * N; ++j) {
                cin >> matrix[i][j];
            }
        }
        ans = true;
        for (int i = 1; i <= N * N; ++i) {
            if (!check_row(i, N * N)) {
                //printf("row = %d n = %d\n", i, N);
                ans = false;
                break;
            }
            if (!check_col(i, N * N)) {
                //printf("col = %d n = %d\n", i, N);
                ans = false;
                break;
            }
            if (!check_matrix(i - 1, N)) {
                //printf("matrix = %d n = %d\n", i - 1, N);
                ans = false;
                break;
            }
        }
        cout << "Case #" << k << ": " << (ans ? "Yes" : "No") << endl;
    }

    return 0;
}
